<?php
namespace app\daohang\controller;

use app\common\controller\Api;

class Json extends Api
{
    // 权限认证
    protected $auth = [
         'check'       => false,
         'none_login'  => ['daohang/json/hits'],
         'none_right'  => [],
         'error_login' => 'daohang/json/index',
         'error_right' => '',
    ];
    
    public function _initialize()
    {
		parent::_initialize();
    }
    
    public function index()
    {
        $this->error(lang('empty'));
    }
    
    public function hits()
    {
        $id = input('id/f', 0);

        $this->safeCheck($id);
        
        $infoHits = dbFindValue('common/Info', ['info_id'=>['eq',$id]], 'info_hits');
        if( !is_null($infoHits) ){
            daohangInfoInc($id, 'info_hits');
            $this->success( lang('success'), ['hits'=>intval($infoHits)+1] );
        }
        
        $this->error( lang('empty'), ['hits'=>0] );
    }
    
    //防频繁刷新
    private function safeCheck($id=0)
    {
        if( !$id ){
            $this->error( lang('empty'), ['hits'=>0], -1);
        }
        
        if( !input('server.HTTP_REFERER') ){
            $this->error( lang('empty'), ['hits'=>0], -2);
        }
        
        if( !strpos(input('server.HTTP_REFERER'), input('server.HTTP_HOST')) ){
            $this->error( lang('empty'), ['hits'=>0], -3);
        }
        
        //客户端唯一标识
        $client = md5('dhhits'.$id.$this->request->ip().$this->request->header('user-agent'));
        //一小时内有效点击一次
        if( DcCache($client) ){
            $this->error( lang('dh_error_rest'), ['hits'=>0], -4);
        }
        //缓存点击数
        DcCache($client, 1, 3600);
    }
}